home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NETWORK.SWG / 0025_Call NETAPI.DLL function.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-25  |  4KB  |  164 lines

  1. (*
  2. From: ROBIN@plato.ucsalf.ac.uk (Robin Bowes)
  3.  
  4. I'm trying to call a function in a Windows .dll from
  5. Turbo Pascal for Windows v1.5.
  6.  
  7. The .dll in question is NETAPI.DLL.  The function I want to call is
  8. defined as follows (in C format):
  9.  
  10. (from Microsoft LAN Manager Programmer's Reference, )
  11.  
  12. NetWkstaGetInfo ( const char far *  pszServer,
  13.        short      sLevel,
  14.        char far *    pbBuffer,
  15.        unsigned short   cbBuffer,
  16.        unsigned short far * pcbTotalAvail
  17.       );
  18.  
  19. where
  20.  
  21. pszServer
  22.  contains the name of the server on which to execute NetWkstGetInfo.
  23.  
  24. sLevel
  25.  specfies the level of detail to be supplied in the return buffer
  26.  
  27. pbBuffer
  28.  points to the buffer in which data is returned
  29.  
  30. cbBuffer
  31.  specifies the size of the buffer pointed to by pbBuffer
  32.  
  33. pcbTotalAvail
  34.  points to an unsigned integer in which the number of bytes of
  35.  information available is returned.
  36.  
  37.  
  38. The detail level I require is 10 which means that the buffer returned
  39. will contain a wksta_info_10 structure which is defined as follows:
  40.  
  41. struct wksta_info_10 {
  42.  char far *  wki10_computername;
  43.  char far *  wki10_username;
  44.  char far *  wki10_langroup;
  45.  unsigned char wki10_ver_major;
  46.  unsigned char wki10_ver_minor;
  47.  char far *  wki10_logon_domain;
  48.  char far *  wki10_oth_domains;
  49. };
  50.  
  51.  
  52. I am having trouble getting this function to work.  It will be a .dll
  53. eventually but for now I'm jsut coding it as a program using WinCrt.
  54.  
  55. My code so far looks something like this:
  56. *)
  57. program Username;
  58.  
  59. uses WinTypes, WinCrt;
  60.  
  61. const
  62.  NERR_BufTooSmall = 2123;
  63.   NERR_Success   = 0;
  64.  
  65. type
  66.  Wksta_info_10 =
  67.   record
  68.    wki10_computername : pChar;
  69.    wki10_username   : pChar;
  70.    wki10_langroup   : pChar;
  71.    wki10_ver_major   : Byte;
  72.    wki10_ver_minor   : Byte;
  73.    wki10_logon_domain : pChar;
  74.    wki10_oth_domains  : pChar;
  75.   end;
  76.  pWksta_info_10 = ^Wksta_info_10;
  77.  
  78. function NetWkstaGetInfo( pszServer     : pChar;
  79.              sLevel      : Integer;
  80.              var pbBuffer   : pWksta_info_10;
  81.              cbBuffer     : Word;
  82.              var pcbTotalAvail : pWord
  83.             ) : Integer; far; external 'NETAPI';
  84.  
  85. function getUsername(var Username : pChar) : Integer;
  86. var
  87.  pWI        : pWksta_info_10;
  88.  sWorkStationInfo : Word;
  89.  pbBufLen     : pWord;
  90.  pbTotalAvail   : pWord;
  91.  uRetCode     : Integer;
  92.  
  93. begin
  94.  {first call will fail but should return the size of the
  95.  buffer needed to hold all the available data}
  96.  getMem(pbBufLen, sizeOf(pbBufLen));
  97.   pwI := nil;
  98.  uRetCode := NetWkstaGetInfo(nil,   {Servername (nil -> local machine)}
  99.                10,    {Reporting level}    
  100.                pWI,   {target buffer for info}
  101.                0,    {Size of target buffer}
  102.                pbBufLen {Count of bytes available}
  103.                );
  104.  {check the return code from the function}
  105.  if (uRetCode = NERR_BufTooSmall) then
  106.   { check available memory }
  107.   begin
  108.   if maxAvail < pbBufLen^ then
  109.    begin
  110.    getUsername := -1;
  111.       Exit
  112.    end
  113.     else
  114.    {allocate memory for buffer to hold information}
  115.    begin
  116.    getMem(pWI, pbBufLen^)
  117.    end
  118.   end
  119.  else
  120.    {Unexpected error returned}
  121.   begin
  122.     {Pass return code back to calling program}
  123.   getUsername := uRetCode;
  124.   Exit
  125.   end;
  126.  
  127.  {second call to get information}
  128.  getMem(pbTotalAvail, sizeOf(pbTotalAvail));
  129.  uRetCode := NetWkstaGetInfo(nil, 10, pWI,  pbBufLen^, pbTotalAvail);
  130.  getUsername := uRetCode;
  131.  if uRetCode = NERR_Success then
  132.    begin
  133.   Username := pWI^.wki10_username;
  134.   end;
  135.  freeMem(pbBufLen, sizeOf(pbBufLen));
  136.  freeMem(pbTotalAvail, sizeOf(pbTotalAvail))
  137. end;
  138.  
  139. {exports
  140.  getUsername  index 1;}
  141.  
  142. var
  143.  retVal : Integer;
  144.  uName : pChar;
  145.  
  146. begin
  147. getMem(uName, sizeOf(uName));
  148. retVal := getUserName(uName);
  149. if retVal = NERR_Success then
  150.  writeln(uName)
  151. else
  152.  writeln('Error returned: ', retVal);
  153. freeMem(uName, sizeOf(uName));
  154. end.
  155. {
  156.  
  157. This compiles OK but throws a GPF in NETAPI.DLL.
  158.  
  159. I'm fairly sure it's the conversion of the structure type that's causing
  160. the problem.
  161.  
  162. Has anybody got any ideas ?
  163.  
  164. }